<?php 
if(!in_array($argv[0], ['controller','logic','model','web','mobile'])) {
    console("缺少create类型：[controller|logic|model|web|mobile]");
    return;
}
if(empty($argv[1])) {
    console("缺少文件名称");
    reutrn;
}

$type = $argv[0];
$name = $argv[1];
$name = strtolower($name);

if($name == 'Common') {
    console("禁止创建的文件名：Common");
    return;
}

// 拼写文件路径
if(in_array($type, ['controller','logic','model'])) {
    $name = ucfirst($name);
    $path = ADDON_PATH . DS . 'application' . DS . $type . DS . $name . '.php';
} else if(in_array($type, ['web','mobile'])) {
    if (strpos('mobile', $name) === 0 || strpos('base', $name) === 0) {
        console("禁止创建的模板：" . $name);
        return;
    }
    if ($type == 'web') {
        $name = str_replace('/', DS, $name);
        $path = ADDON_PATH . DS . 'template' . DS . $name . '.html';
    } else {
        $name = str_replace('/', DS, $name);
        $path = ADDON_PATH . DS . 'template' . DS . 'mobile' . DS . $name . '.html';
    }
}

if(is_file($path)) {
    console("文件已存在，创建失败：" . $path);
    return;
}

// 检查创建文件的文件夹是否存在
$dir = dirname($path);
if (!is_dir($dir)) {
    mkdir($dir, 0755, true);
}

// 创建模板
if ($type == 'controller') {
    $content = "<?php
namespace wsys\\controller;

class $name extends Common
{

    public function index(&\$w, \$get, \$post)
    {
        return 'index';
    }

}
";
}

if ($type == 'logic') {
    $content = "<?php
namespace wsys\\logic;

class $name extends Common
{

    public function getList(\$page, \$condition = [])
    {
        \$data = [];
        return \$this->setInfo(0, 'getList', \$data);
    }

}
";
}

if ($type == 'model') {
    $content = "<?php
namespace wsys\\model;

class $name extends Common
{

    public function getList(\$page = false, \$size = false, \$condition = [])
    {
        \$list = \$this->getall();
        return \$list;
    }

    public function findRow(\$condition = [])
    {
        \$row = \$this->find();
        return \$row;
    }

}
";
}

if ($type == 'web') {
    $content = "{template 'base/common-header'}



{template 'base/common-footer'}
";
}

if ($type == 'mobile') {
    $content = '<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>wsys</title>
    {template "base/header-config"}
    <link rel="stylesheet" href="{WEB_RESOURCE}/mobile/default/index.min.css?v=1">
</head>

<body>
    <div id="app"></div>
    <script src="{WEB_RESOURCE}/mobile/default/index.min.js?v=1"></script>
</body>

</html>';
}

$result = file_put_contents($path, $content);

if($result === false) {
    console("创建失败，请检查项目是否允许写入");
    return;
}

console("创建成功：" . $path);
